// pages/event-add/index.js
import {
  curTimeAll
} from '../../third-utils/time/index.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    curEventId:'',
		showModal: false,
		modalType: '1',
		lastFoodTime:'',
		recordDate:'',
		startDate: '2020-01-01 00:00',
		categroyList: [],
		curCate: 0,
		bb_shape: {},
		bb_qk: {},
		bb_color: {},
		bb_type: {},
		shapeIndex: -1,
		colorIndex: 0,
		typeIndex: '',
		qkIndex: -1,
		imageAttr: [],
		ot_l: {},
		postData: {},
		babyId: '',
		babyInfo: {},
		otlIndex: -1,
		ot_color: {},
		birthday: '',
		otcolorIndex: 0,
		ppColor: '#fffbeb',
		bbColorAttr: [{
				value: '#fffbeb',
				title: '灰白',
			}, {
				value: '#c4bf78',
				title: '黄绿',
			}, {
				value: '#e6cb7c',
				title: '黄色',
			}, {
				value: '#9e7861',
				title: '棕色',
			},
			{
				value: '#4b4b4b',
				title: '黑色',
			}, {
				value: '#e28476',
				title: '红色',
			}
		],

		otColor: '#f1f1f1',
		otColorAttr: [{
				value: '#f1f1f1',
				title: '清澈',
			}, {
				value: '#ffffff',
				title: '白色',
			}, {
				value: '#f3e077',
				title: '黄色',
			}, {
				value: '#caca24',
				title: '绿色',
			}, {
				value: '#f29d9d',
				title: '粉红色',
			}, {
				value: '#ab4646',
				title: '红色',
			}, {
				value: '#a17a51',
				title: '棕色',
			}, {
				value: '#6a3c1c',
				title: '咖色',
			}
		],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    console.log(curTimeAll, 12)
    this.setData({
      recordDate: curTimeAll
    })
    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/api/recordType/findList',
      data: {
        parentId: e.cateId
      },
      method: 'post',
      success: (res) => {
        if (res.data.code == 200) {
          this.setData({
            categroyList: res.data.data
          })
          
          if (e.typeId) {

            this.data.categroyList.some((item, index) => {
                if (item.id == e.typeId) {
                    this.setData({
                        curCate: index
                    })
                }
            })
        }
        if(e.id){
            this.setData({
                curEventId:e.id
            })
            this.getEventInfo(e.id)
        }
        }
      }
    })
    this.initData()
  },

  

  getEventInfo(id) {

    wx.request({
        url: 'https://babyplanet.isv-dev.youzan.com/api/babyRecord/view',
        data: {
            id: id
        },
        method: 'get',
        success: (res) => {
            if (res.data.code == 200) {
                this.setDefault(res.data.data.detail)
            }
        }
    })
},
handleDel(){
    
let that=this
wx.showModal({
  title: '提示',
  content: '确定删除该记录吗？',
  success (res) {
    if (res.confirm) {
      wx.request({
        url: 'https://babyplanet.isv-dev.youzan.com/api/babyRecord/delete',
        data: {
          id:that.data.curEventId
        },
        method: 'post',
        success: (res) => {
          if (res.data.code == 200) {

            wx.showToast({
                title: '删除成功！',
                icon: 'none',
                duration: 1500 //持续的时间
            })
            setTimeout(() => {
                wx.navigateBack()
            }, 1500)
          }
        }
      })
    } else if (res.cancel) {
      console.log('用户点击取消')
    }
  }
})
},
setDefault(curData) {
    if(this.data.curEventId){
        
    this.setData({
        recordDate: curData.recordDate,
        description:curData.description,
        cateType1Index:1,
        lastFood:curData.lastFood,
        lastFoodTime:curData.lastFoodTime,
        
        imageAttr:curData.picture?(curData.picture).split(','):''
    })
    }

    if (curData.type) {
        for (let i in this.data.bb_type.opts) {
            if (this.data.bb_type.opts[i].name == curData.type) {
                this.setData({
                  typeIndex: i
                })
            }
        }
        for (let i in this.data.ot_l.opts) {
            if (this.data.ot_l.opts[i].name == curData.type) {
                this.setData({
                  otlIndex: i
                })
            }
        }
    }
    if (curData.shape) {
        for (let i in this.data.bb_shape.opts) {
            if (this.data.bb_shape.opts[i].name == curData.shape) {
                this.setData({
                  shapeIndex: i
                })
            }
        }
    }
    if (curData.color) {
        for (let i in this.data.bbColorAttr) {
            if (this.data.bbColorAttr[i].title == curData.color) {
                this.setData({
                  colorIndex: i,
                  ppColor:this.data.bbColorAttr[i].value 
                })
            }
        }
        for (let i in this.data.otColorAttr) {
            if (this.data.otColorAttr[i].title == curData.color) {
                this.setData({
                  otcolorIndex: i,
                  otColor:this.data.otColorAttr[i].value 
                })
            }
        }
    }
    if (curData.remark) {
        for (let i in this.data.bb_qk.opts) {
            if (this.data.bb_qk.opts[i].name == curData.remark) {
                this.setData({
                  qkIndex: i
                })
            }
        }
    }
},
  slider2change() {},
  select(e) {
    this.setData({
      [e.currentTarget.dataset.key]: e.currentTarget.dataset.index
    })
  },
  hideModal() {
    this.setData({
      showModal: false,
    })
  },
  showModal(e) {
    this.setData({
      modalType:e.currentTarget.dataset.key,
      showModal: true,
    })
  },
  
  formSubmit(e) {
    console.log(e, 12122)
    if (!this.data.recordDate) {
      wx.showToast({
        title: '请选择记录时间',
        icon: 'none',
        duration: 1200 //持续的时间
      })
      return
    }
    let post = {
      babyId: 32,
      typeId: this.data.categroyList[this.data.curCate].id,

    }

    if (this.data.curCate == 0) {

      if (this.data.typeIndex == '') {
        wx.showToast({
          title: '请选择排便情况',
          icon: 'none',
          duration: 1200
        })
        return
      }
      if (this.data.shapeIndex == -1) {
        wx.showToast({
          title: '请选择便便形状',
          icon: 'none',
          duration: 1200
        })
        return
      }
      if (this.data.qkIndex == -1) {
        wx.showToast({
          title: '请选择排便情况',
          icon: 'none',
          duration: 1200
        })
        return
      }
      post.detail = {
        recordDate: this.data.recordDate,
        type: this.data.bb_type.opts[this.data.typeIndex].name,
        shape: this.data.bb_shape.opts[this.data.shapeIndex].name,
        color: this.data.bbColorAttr[this.data.colorIndex].title,
        remark: this.data.bb_qk.opts[this.data.qkIndex].name,
        description: e.detail.value.description,
        picture: String(this.data.imageAttr)
      }
    } else if (this.data.curCate == 1) {


      if (this.data.otlIndex == -1) {
        wx.showToast({
          title: '请选择呕吐物的量',
          icon: 'none',
          duration: 1200
        })
        return
      }

      post.detail = {
        recordDate: this.data.recordDate,
        type: this.data.ot_l.opts[this.data.otlIndex].name,
        color: this.data.otColorAttr[this.data.otcolorIndex].title,
        lastFood: e.detail.value.lastFood,
        lastFoodTime: '2023-06-11 12:05',
        description: e.detail.value.description,
        picture: String(this.data.imageAttr)
      }

      if (!wx.getStorageSync('lastFoodTips') && !e.detail.value.lastFood) {
        wx.setStorageSync('lastFoodTips', '1')
        this.setData({
          modalType:'1',
          showModal: true,
          postData:post
        })
        console.log(this.data.postData)
        return
      }
    }
    
    let apiUrl='https://babyplanet.isv-dev.youzan.com/api/babyRecord/add'
    if(this.data.curEventId){
        apiUrl="https://babyplanet.isv-dev.youzan.com/api/babyRecord/update"
        post.id=this.data.curEventId
    }
    wx.request({
      url: apiUrl,
      data: post,
      method: 'post',
      success: (res) => {
        console.log(res, 122)
        if (res.data.code == 200) {

          wx.showToast({
            title: '保存成功！',
            icon: 'none',
            duration: 1500 //持续的时间
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      }
    })
  },
  toSave(){
    wx.request({
      url: 'https://babyplanet.isv-dev.youzan.com/api/babyRecord/add',
      data: this.data.postData,
      method: 'post',
      success: (res) => {
        console.log(res, 122)
        if (res.data.code == 200) {

          wx.showToast({
            title: '保存成功！',
            icon: 'none',
            duration: 1500 //持续的时间
          })
          setTimeout(() => {

            wx.navigateTo({
              url: '/pages/index/index',
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      }
    })
  },
  pickerChangeType(e) {
    console.log(e, 1111)

    this.setData({
      [e.currentTarget.dataset.key]:e.detail.value
    })
    if(e.currentTarget.dataset.key=='colorIndex'){
      this.setData({
        ppColor:this.data.bbColorAttr[e.detail.value].value
      })
    }
    if(e.currentTarget.dataset.key=='otcolorIndex'){
      this.setData({
        otColor:this.data.otColorAttr[e.detail.value].value
      })
    }
  },
  changeCate(e) {
    this.setData({
      curCate: e.currentTarget.dataset.index
    })
  },
  delImg(e) {
    this.data.imageAttr.splice(e.currentTarget.dataset.index, 1)
    this.setData({
      imageAttr: this.data.imageAttr
    })
  },
  addImg() {
    wx.chooseImage({
      count: 9,
      success: (chooseImageRes) => {
        console.log(chooseImageRes)
        const tempFilePaths = chooseImageRes.tempFilePaths;
        tempFilePaths.some((item) => {
          wx.uploadFile({
            url: 'https://babyplanet.isv-dev.youzan.com/common/image/upload', //仅为示例，非真实的接口地址
            filePath: item,
            fileType: 'image',
            name: 'file',
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'multipart/form-data',
            },
            success: (uploadFileRes) => {
              let res = JSON.parse(uploadFileRes.data)
              if (res.code == 200) {
                this.data.imageAttr.push(res.data.imageUrl)
                this.setData({
                  imageAttr: this.data.imageAttr
                })
              }
            },
          });
        })
      }
    });
  },
  initData() {
    let codeAttr = ['bb_shape', 'bb_qk', 'bb_type', 'bb_color', 'ot_l', 'ot_color']
    codeAttr.some(item => {
      wx.request({
        url: 'https://babyplanet.isv-dev.youzan.com/api/selectConfig/view',
        data: {
          code: item
        },
        method: 'get',
        success: (res) => {
          if (res.data.code == 200) {
            this.setData({
              [item]: res.data.data
            })
          }
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})